<template>
  <view class="input-field-container">
    <!-- 页头 -->
    <view class="header">
      <view class="header-content">
        <view class="back-btn" @click="goBack">
          <image class="back-icon" src="/static/icons/candidate/pre_page.png"></image>
        </view>
        <text class="header-title">{{ pageTitle }}</text>
        <view class="save-btn" @click="saveField">
          <text class="save-text">确定</text>
        </view>
      </view>
    </view>

    <!-- 输入区域 -->
    <view class="input-section">
      <input
        v-if="fieldType !== 'content'"
        v-model="inputValue"
        :placeholder="placeholder"
        class="input-field"
        :maxlength="maxLength"
        type="text"
      />
      <textarea
        v-else
        v-model="inputValue"
        :placeholder="placeholder"
        class="textarea-field"
        :maxlength="maxLength"
        auto-height
      />
      <view class="char-count">
        <text class="count-text">{{ inputValue.length }}/{{ maxLength }}</text>
      </view>
    </view>
  </view>
</template>

<script setup>
import { ref, onMounted, computed } from 'vue'

// 输入值
const inputValue = ref('')

// 字段类型和配置
const fieldType = ref('')
const fieldConfig = ref({})

// 页面标题
const pageTitle = computed(() => {
  return fieldConfig.value.title || '输入'
})

// 占位符文本
const placeholder = computed(() => {
  return fieldConfig.value.placeholder || '请输入'
})

// 最大长度
const maxLength = computed(() => {
  return fieldConfig.value.maxLength || 50
})

// 字段配置映射
const fieldConfigs = {
  companyName: {
    title: '公司名称',
    placeholder: '请输入公司名称',
    maxLength: 50,
    field: 'companyName',
    event: 'updateCompanyName'
  },
  position: {
    title: '职位名称',
    placeholder: '请输入职位名称',
    maxLength: 30,
    field: 'position',
    event: 'updatePosition'
  },
  content: {
    title: '工作内容',
    placeholder: '请输入工作内容',
    maxLength: 200,
    field: 'content',
    event: 'updateContent'
  },
  department: {
    title: '所属部门',
    placeholder: '请输入所属部门',
    maxLength: 30,
    field: 'department',
    event: 'updateDepartment'
  },
  projectName: {
    title: '项目名称',
    placeholder: '请输入项目名称',
    maxLength: 50,
    field: 'projectName',
    event: 'updateProjectName'
  },
  roleName: {
    title: '担任角色',
    placeholder: '请输入担任角色',
    maxLength: 30,
    field: 'roleName',
    event: 'updateRoleName'
  },
  projectLink: {
    title: '项目链接',
    placeholder: '请输入项目链接',
    maxLength: 200,
    field: 'projectLink',
    event: 'updateProjectLink'
  },
  school: {
    title: '学校名称',
    placeholder: '请输入学校名称',
    maxLength: 50,
    field: 'school',
    event: 'updateSchool'
  },
  major: {
    title: '专业',
    placeholder: '请输入专业',
    maxLength: 50,
    field: 'major',
    event: 'updateMajor'
  },
  jobName: {
    title: '职位名称',
    placeholder: '请输入职位名称',
    maxLength: 32,
    field: 'jobName',
    event: 'updateJobName'
  },
  address: {
    title: '工作地点',
    placeholder: '请填写真实工作地点(具体到园区门牌号)',
    maxLength: 64,
    field: 'address',
    event: 'updateAddress'
  }
}

// 页面加载时获取参数
onMounted(() => {
  const pages = getCurrentPages()
  const currentPage = pages[pages.length - 1]
  const options = currentPage.options
  
  // 获取字段类型
  if (options.type) {
    fieldType.value = options.type
    fieldConfig.value = fieldConfigs[options.type] || {}
  }
  
  // 获取传入的值
  if (options.value) {
    inputValue.value = decodeURIComponent(options.value)
  }
})

// 返回上一页
const goBack = () => {
  uni.navigateBack()
}

// 确定按钮
const saveField = () => {
  if (!inputValue.value.trim()) {
    uni.showToast({
      title: `请输入${fieldConfig.value.title}`,
      icon: 'none'
    })
    return
  }
  
  // 返回上一页并传递数据
  const pages = getCurrentPages()
  if (pages.length > 1) {
    const prevPage = pages[pages.length - 2]
    
    // 方法1：通过页面实例直接调用方法
    if (prevPage && prevPage.$vm && prevPage.$vm.updateFieldValue) {
      prevPage.$vm.updateFieldValue(fieldConfig.value.field, inputValue.value)
    }
    
    // 方法2：通过全局事件总线传递数据（备用方案）
    if (fieldConfig.value.event) {
      uni.$emit(fieldConfig.value.event, inputValue.value)
    }
  }
  
  uni.navigateBack()
}
</script>

<style scoped lang="less">
.input-field-container {
  background-color: #f5f5f5;
  min-height: 100vh;
}

/* 页头样式 */
.header {
  background-color: #ffffff;
  border-bottom: 1px solid #f0f0f0;
  position: sticky;
  top: 0;
  z-index: 100;
}

.header-content {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 20rpx 30rpx;
  height: 88rpx;
}

.back-btn {
  width: 60rpx;
  height: 60rpx;
  display: flex;
  align-items: center;
  justify-content: center;
}

.back-icon {
  width: 48rpx;
  height: 48rpx;
}

.header-title {
  font-size: 36rpx;
  font-weight: 500;
  color: #333;
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Helvetica Neue', Arial, sans-serif;
}

.save-btn {
  height: 60rpx;
  display: flex;
  align-items: center;
  justify-content: center;
}

.save-text {
  font-size: 32rpx;
  color: #333;
  font-weight: 400;
}

/* 输入区域样式 */
.input-section {
  background-color: #ffffff;
  border-radius: 12rpx;
  padding: 30rpx;
}

.input-field {
  width: 100%;
  height: 80rpx;
  font-size: 32rpx;
  color: #333;
  font-weight: 400;
  border: none;
  outline: none;
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Helvetica Neue', Arial, sans-serif;
}

.textarea-field {
  width: 100%;
  min-height: 120rpx;
  font-size: 32rpx;
  color: #333;
  font-weight: 400;
  border: none;
  outline: none;
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Helvetica Neue', Arial, sans-serif;
  line-height: 1.5;
  resize: none;
}

.char-count {
  display: flex;
  justify-content: flex-end;
  margin-top: 20rpx;
  padding-top: 20rpx;
  border-top: 1px solid #f0f0f0;
}

.count-text {
  font-size: 24rpx;
  color: #00bdb4;
  font-weight: 400;
}
</style>
